package icu.zqyhdm.blog.controller;

import icu.zqyhdm.blog.entity.Type;
import icu.zqyhdm.blog.service.TypeService;
import icu.zqyhdm.blog.vo.TypeVo;
import icu.zqyhdm.helper.JwtHelper;
import icu.zqyhdm.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * @Description
 * @Author lifu
 * @Date 2021/6/22 21:56
 */
@Api(tags = "文章类型的接口")
@RestController
@RequestMapping(value = "/api/blog/type")
public class TypeController {
    @Autowired
    private TypeService typeService;

    @ApiOperation(value = "查询所有文章类型，类型名去重，并根据类型对应的文章数量降序排")
    @GetMapping(value = "/list")
    public Result List(){
        List<TypeVo> typeList = typeService.getAllType();
        return Result.ok(typeList);
    }

    @ApiOperation(value = "查询当前用户的所有文章类型，并根据类型对应的文章数量降序排")
    @GetMapping(value = "/myList")
    public Result List(@RequestHeader("X-Token") String token){
        Integer userId = JwtHelper.getUserId(token);
        List<TypeVo> typeList = typeService.getTypeByUserId(userId);
        return Result.ok(typeList);
    }

    @ApiOperation(value = "查询某个用户的所有文章类型，并根据类型对应的文章数量降序排")
    @GetMapping(value = "/list/{userId}")
    public Result List(@PathVariable Integer userId){
        List<TypeVo> typeList = typeService.getTypeByUserId(userId);
        return Result.ok(typeList);
    }

    @ApiOperation(value = "增加类型")
    @PostMapping(value = "/add")
    public Result add(@RequestHeader("X-Token") String token, @RequestParam String typeName){
        typeService.saveType(token,typeName);
        return Result.ok();
    }
    @ApiOperation(value = "修改类型")
    @PutMapping(value = "/update")
    public Result update(@RequestHeader("X-Token") String token, @Valid @RequestBody Type type){
        typeService.updateType(token,type);
        return Result.ok();
    }

    @ApiOperation(value = "删除类型")
    @DeleteMapping(value = "/delete/{id}")
    public Result delete(@RequestHeader("X-Token") String token, @PathVariable("id") Integer typeId){
        typeService.deleteType(token,typeId);
        return Result.ok();
    }

    @ApiOperation(value = "查询标签")
    @GetMapping(value = "/show/{typeId}")
    public Result add(@PathVariable Integer typeId){
        TypeVo typeVo = typeService.getTypeById(typeId);
        return Result.ok(typeVo);
    }

}
